package org.zffc.common.util;

import com.alibaba.fastjson.JSONArray;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 基础工具类
 */
public class CommonUtil {
    /**
     * 判断对象是否不为空
     * @param object
     * @return
     * @author zhangfei
     */
    public static boolean isObjectNotEmpty(Object object) {
        return !isObjectEmpty(object);
    }


    /**
     * 判断对象是否为空 <br/>
     * 支持各种类型的对象
     * @param obj
     * @return
     * @author zhangfei
     */
    public static boolean isObjectEmpty(Object obj) {
        if (null == obj) {
            return true;
        }
        if (obj instanceof CharSequence) {
            return isEmpty(obj);
        } else if (obj instanceof Map) {
            return ((Map<?, ?>) obj).isEmpty();
        } else if (obj instanceof Iterable) {
            return isObjectEmpty(((Iterable<?>) obj).iterator());
        } else if (obj instanceof Iterator) {
            return !((Iterator<?>) obj).hasNext();
        } else if (isArray(obj)) {
            return 0 == Array.getLength(obj);
        }
        return false;
    }

    /**
     * 判断一个字符串是否是空
     * @param object
     * @return
     */
    public static boolean isEmpty(Object object) {
        if (object == null) {
            return (true);
        }
        if ("".equals(object)) {
            return (true);
        }
        return (false);
    }

    /**
     * 判断字符串非空
     * @param object
     * @return
     */
    public static boolean isNotEmpty(Object object) {
        if (object != null && !"".equals(object)) {
            return (true);
        }
        return (false);
    }

    /**
     * 方法描述 判断一个对象是否是一个数组
     *
     * @param obj
     * @return
     * @author zhangfei
     */
    public static boolean isArray(Object obj) {
        if (obj == null) {
            return false;
        }
        return obj.getClass().isArray();
    }

    /**
     * 判断旧值与新值 是否相等
     *
     * @param oldVal
     * @param newVal
     * @return
     */
    public static boolean isEqual(Object oldVal, Object newVal) {
        if (oldVal != null && newVal != null) {
            if (isArray(oldVal)) {
                return equalityOfArrays((Object[]) oldVal, (Object[]) newVal);
            }else if(oldVal instanceof JSONArray){
                if(newVal instanceof JSONArray){
                    return equalityOfJSONArray((JSONArray) oldVal, (JSONArray) newVal);
                }else{
                    if (isEmpty(newVal) && (oldVal == null || ((JSONArray) oldVal).size() == 0)) {
                        return true;
                    }
                    List<Object> arrayStr = Arrays.asList(newVal.toString().split(","));
                    JSONArray newValArray = new JSONArray(arrayStr);
                    return equalityOfJSONArray((JSONArray) oldVal, newValArray);
                }
            }else{
                return oldVal.equals(newVal);
            }

        } else {
            if (oldVal == null && newVal == null) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 判断两个数组是否相等（数组元素不分顺序）
     *
     * @param oldVal
     * @param newVal
     * @return
     */
    public static boolean equalityOfArrays(Object[] oldVal, Object newVal[]) {
        if (oldVal != null && newVal != null) {
            Arrays.sort(oldVal);
            Arrays.sort(newVal);
            return Arrays.equals(oldVal, newVal);
        } else {
            if ((oldVal == null || oldVal.length == 0) && (newVal == null || newVal.length == 0)) {
                return true;
            } else {
                return false;
            }
        }
    }

    /**
     * 判断两个数组是否相等（数组元素不分顺序）
     *
     * @param oldVal
     * @param newVal
     * @return
     */
    public static boolean equalityOfJSONArray(JSONArray oldVal, JSONArray newVal) {
        if (oldVal != null && newVal != null) {
            Object[] oldValArray = oldVal.toArray();
            Object[] newValArray = newVal.toArray();
            return equalityOfArrays(oldValArray,newValArray);
        } else {
            if ((oldVal == null || oldVal.size() == 0) && (newVal == null || newVal.size() == 0)) {
                return true;
            } else {
                return false;
            }
        }
    }

}
